@using AntDesign.Internal
@using AntDesign.Select.Internal
@namespace AntDesign
@inherits SelectBase<TItemValue, TItem>
@typeparam TItemValue
@typeparam TItem

<CascadingValue Value="this" IsFixed="@(!IsGroupingEnabled)">
    <CascadingValue Value=@("ant-select-dropdown") Name="PrefixCls" IsFixed>
            <OverlayTrigger @ref="@_dropDown"
                            Visible="Open"
                            Disabled="Disabled"
                            Trigger="new[] { Trigger.Click }"
                            HiddenMode
                            BoundaryAdjustMode="@BoundaryAdjustMode"
                            OnMouseEnter="@(() => { OnMouseEnter?.Invoke(); })"
                            OnMouseLeave="@(() => { OnMouseLeave?.Invoke(); })"
                            OnVisibleChange="@OnOverlayVisibleChangeAsync"
                            PopupContainerSelector="@PopupContainerSelector"
                            OverlayEnterCls="ant-slide-up-enter ant-slide-up-enter-active ant-slide-up"
                            OverlayLeaveCls="ant-slide-up-leave ant-slide-up-leave-active ant-slide-up">
                <Overlay>
                    <div style="@_dropdownStyle">
                        @if (DropdownRender != null)
                        {
                            @DropdownRender(RenderOptionDropdown())
                        }
                        else
                        {
                            @RenderOptionDropdown()
                        }
                    </div>                    
                </Overlay>
                <Unbound>
                    <CascadingValue Value="this" Name=@("ParentSelect") IsFixed>
                        <CascadingValue Value="@LabelTemplate" Name="ParentLabelTemplate">
                            <CascadingValue Value="@MaxTagPlaceholder" Name="ParentMaxTagPlaceholerTemplate">
                                <CascadingValue Value="@ShowSearchIcon" Name="ShowSearchIcon">
                                    <CascadingValue Value="@ShowArrowIcon" Name="ShowArrowIcon">
                                        <SelectContent Prefix="@ClassPrefix"
                                                       RefBack="@context"
                                                       @ref="_selectContent"
                                                       TItemValue="TItemValue"
                                                       TItem="TItem"
                                                       SearchValue="@_searchValue"
                                                       SearchDebounceMilliseconds="@SearchDebounceMilliseconds"
                                                       IsOverlayShow="@_dropDown.IsOverlayShow()"
                                                       MaxTagCount="@_maxTagCountAsInt"
                                                       OnInput="@OnInputAsync"
                                                       OnKeyUp="@OnKeyUpAsync"
                                                       OnKeyDown="@OnKeyDownAsync"
                                                       OnFocus="@OnInputFocusAsync"
                                                       OnBlur="@OnInputBlurAsync"
                                                       OnClearClick="@OnInputClearClickAsync"
                                                       OnRemoveSelected="@OnRemoveSelectedAsync"
                                                       Placeholder="@Placeholder" />
                                    </CascadingValue>
                                </CascadingValue>
                            </CascadingValue>
                        </CascadingValue>
                    </CascadingValue>
                </Unbound>
            </OverlayTrigger>
    </CascadingValue>
</CascadingValue>

@code
{
    static RenderFragment<(Select<TItemValue, TItem> select, RenderFragment<TItem> itemTemplate)> selectOptionsRender = ctx =>
        @<Template>
@{ #if NET5_0_OR_GREATER }
            @if(ctx.select.EnableVirtualization)
            {
                <Microsoft.AspNetCore.Components.Web.Virtualization.Virtualize 
                    Items=@ctx.select.SortedSelectOptionItems.Where(x => !x.IsHidden).ToList()
                    ChildContent="optionRender(ctx.itemTemplate)"/>
            }
            else
            {
#endif 
                <ForeachLoop 
                    Items=@ctx.select.SortedSelectOptionItems
                    ChildContent="optionRender(ctx.itemTemplate)"/>
#if NET5_0_OR_GREATER 
            }
@{ #endif }
        </Template>;

    static Func<RenderFragment<TItem>, RenderFragment<SelectOptionItem<TItemValue, TItem>>> optionRender = 
        itemTemplate => option =>
        @<CascadingValue Value=@itemTemplate Name="ItemTemplate">
            <CascadingValue Value="@option.InternalId" Name="InternalId">
                <SelectOption @key="@option.InternalId"
                    TItemValue="TItemValue"
                    TItem="TItem">
                </SelectOption>
            </CascadingValue>
        </CascadingValue>;

    RenderFragment RenderOptionDropdown()
    {
        @if (SelectOptions != null)
        {
            return @<div class="" style="max-height: @PopupContainerMaxHeight; overflow-y: auto;" @ref="_scrollableSelectDiv">
                <div>
                    <div class="" role="listbox" style="@ListboxStyle">
                        @if (CustomTagSelectOptionItem != null)
                        {
                            <CascadingValue Value="@ItemTemplate" Name="ItemTemplate">
                                <CascadingValue Value="@CustomTagSelectOptionItem" Name="Model">
                                    <SelectOption @key="@CustomTagSelectOptionItem.InternalId"
                                                  TItemValue="TItemValue"
                                                  TItem="TItem">
                                    </SelectOption>
                                </CascadingValue>
                            </CascadingValue>
                        }
                        @SelectOptions
                        @if (AddedTags != null)
                        {
                            foreach (var selectOption in AddedTags)
                            {
                                <CascadingValue Value="@ItemTemplate" Name="ItemTemplate">
                                    <CascadingValue Value="@selectOption" Name="Model">
                                        <SelectOption @key="@selectOption.InternalId"
                                                      TItemValue="TItemValue"
                                                      TItem="TItem">
                                        </SelectOption>
                                    </CascadingValue>
                                </CascadingValue>
                            }
                        }
                    </div>
                </div>
            </div>
    ;
        }
        else if (SelectOptions == null && !AllOptionsHidden())
        {
            return@<div class="" style="max-height: @PopupContainerMaxHeight; overflow-y: auto;" @ref="_scrollableSelectDiv">
                <div>
                    <div class="" role="listbox" style="@ListboxStyle">
                        @{
                            @if (!IsGroupingEnabled)
                            {
                                @selectOptionsRender((this, ItemTemplate))
                            }
                            else
                            {
                                <CascadingValue Value="@ItemTemplate" Name="ItemTemplate">
                                    <SelectOptionGroup TItemValue="TItemValue" TItem="TItem"></SelectOptionGroup>
                                </CascadingValue>
                            }
                        }
                    </div>
                </div>
            </div>;
        }
        @if (AllOptionsHidden())
        {
            return@<div role="listbox" id="@(Id)_list" class="@ClassPrefix-item-empty">
                @if (NotFoundContent != null)
                {
                    @NotFoundContent
                }
                else
                {
                    <Empty Simple Small />
                }
            </div>
    ;
        }

        return builder => { };
    }

}
